class DiscussionMessage <  ActiveRecord::Base
  establish_connection :discourse_test
  set_table_name 'discussion_messages'
  
  #ruby script\runner DiscussionMessage.migrate
  def self.migrate
    dmes = DiscussionMessage.find_by_sql(["SELECT d.title, d.description, m.title AS m_title, m.text FROM discussions d, messages m, discussion_messages dm WHERE d.discussion_id = dm.discussion_id AND m.message_id = dm.message_id"])
    
    dmes.each do |dm|
      d = Discussion.find_by_title(dm.title)
      d = Discussion.create(:title => dm.title, :content => "#{dm.description}")if d.nil?
      m = Message.find_by_title(dm.m_title)
      m = Message.create(:title => dm.m_title, :content => "#{dm.text}") if m.nil?
      Relation.create("object_id"=>d.id, "subject_id"=>m.id)
    end
  end
end
